/*
* 一个遵守流控，按照注入率和排放率随机处理数据的路由器行为模型，
* 用于给mac加激励。
*/

module router_behav #(
    parameter IR=70,    // inject rate
    parameter ER=50     // eject rate
)(
    input clk, // noc clk, 800~1000 MHz
    input rst,
    output reg o_valid,
    output reg [127:0] o_data,
    input i_yummy,
    input i_valid,
    input [127:0] i_data,
    output reg o_yummy
);
    localparam CR = 4;
    reg [2:0] ssu, dsu;
    always @(posedge clk) begin
        if(rst) 
            dsu<=0;
        else if(o_valid && i_yummy)
            ;
        else if(o_valid)
            dsu<=dsu+1;
        else if(i_yummy)
            dsu<=dsu-1;
    end
    always @(posedge clk) begin
        if(rst)
            ssu<=0;
        else if(i_valid && o_yummy)
            ;
        else if(i_valid)
            ssu<=ssu+1;
        else if(o_yummy)
            ssu<=ssu-1;
    end

    always @(posedge clk) begin
        if(rst)
            o_valid<=0;
        else if(dsu<CR-1)
            o_valid<={$random}%100<IR;
        else
            o_valid<=0;
    end
    always @(posedge clk) begin
        if(rst)
            o_yummy<=0;
        else if(ssu>0)
            o_yummy<={$random}%100<ER;
        else
            o_yummy<=0;
    end

    always @(posedge clk) begin
        if(rst)
            o_data<=0;
        else
            o_data<={4{$random}};
    end
endmodule

